<template>
  <div class="operation-log-container">
    <!-- 日期选择器 -->
    <el-date-picker
        v-model="dateRange"
        type="daterange"
        range-separator="至"
        start-placeholder="开始日期"
        end-placeholder="结束日期"
        @change="fetchOperationLogs"
    ></el-date-picker>
    <!-- 加载中提示 -->
    <div v-if="loading">加载中...</div>
    <!-- 错误提示 -->
    <div v-else-if="errorMessage">{{ errorMessage }}</div>
    <!-- 无数据提示 -->
    <div v-else-if="operationLogs.length === 0">暂无数据</div>
    <!-- 操作记录表格 -->
    <el-table v-else :data="operationLogs" style="width: 100%">
      <el-table-column prop="id" label="ID" width="120" />
      <el-table-column label="员工姓名" width="120">
        <template #default="scope">
          {{ scope.row.employeeName || '未知' }}
        </template>
      </el-table-column>
      <el-table-column prop="operationTime" label="操作时间" width="200" />
      <el-table-column prop="operationType" label="操作类型" width="150" />
      <el-table-column prop="operationDetail" label="操作详情" />
    </el-table>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue';
import http from '@/axios/http';

const dateRange = ref([]); // 日期范围绑定的值
const operationLogs = ref([]); // 操作记录数据
const loading = ref(false); // 加载状态
const errorMessage = ref(''); // 错误信息

// 时间戳转换函数
const formatTime = (timestamp) => {
  const date = new Date(timestamp);
  const year = date.getFullYear();
  const month = String(date.getMonth() + 1).padStart(2, '0');
  const day = String(date.getDate()).padStart(2, '0');
  const hours = String(date.getHours()).padStart(2, '0');
  const minutes = String(date.getMinutes()).padStart(2, '0');
  const seconds = String(date.getSeconds()).padStart(2, '0');
  return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
};

// 获取操作记录的方法
const fetchOperationLogs = async () => {
  loading.value = true; // 开始加载
  errorMessage.value = ''; // 清空错误信息
  let startDate = null;
  let endDate = null;
  if (dateRange.value.length === 2) {
    // 转换为 yyyy-MM-dd 格式
    startDate = dateRange.value[0].toISOString().split('T')[0];
    endDate = dateRange.value[1].toISOString().split('T')[0];
  }

  try {
    const response = await http.get('/glsb/logsWithEmployeeName', {
      params: {
        startDate: startDate,
        endDate: endDate,
      },
    });
    operationLogs.value = response.data.data.map(item => ({
      ...item,
      operationTime: formatTime(item.operationTime), // 格式化时间戳
      employeeName: item.employeeName || '未知', // 防止字段不存在
    }));
  } catch (error) {
    errorMessage.value = '获取操作记录失败，请稍后重试';
    console.error('获取操作记录失败:', error);
  } finally {
    loading.value = false; // 结束加载
  }
};

// 页面加载时自动获取操作记录
onMounted(fetchOperationLogs);
</script>

<style scoped>
.operation-log-container {
  padding: 20px;
  background-color: #f5f5f5;
  border-radius: 8px;
  box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
}

.el-table {
  margin-top: 20px;
  border-radius: 8px;
  overflow: hidden;
}

.el-table th,
.el-table td {
  text-align: center;
  padding: 10px;
  border-bottom: 1px solid #e0e0e0;
}

.el-table th {
  background-color: #f5f5f5;
  font-weight: bold;
}

.el-table tr:nth-child(even) {
  background-color: #f9f9f9;
}
</style>